package com.dtf.core.common.dao.mysql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;

import com.dtf.core.common.dao.BaseDaoSupport;
import com.dtf.core.common.dao.mysql.MySQLUtils;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.vo.Pagination;
/**
 * 提供
 * @author Administrator
 *
 */
@Component("baseDaoSupport")
public class BaseDaoSupportForMysql extends JdbcDaoSupport implements  BaseDaoSupport{
	
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(){
		if (namedParameterJdbcTemplate == null) {
			namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(super.getJdbcTemplate());
		}
		return namedParameterJdbcTemplate;
	}
	
	@Override
	public Pagination queryListMapForPage(String sql, List param, int pageSize, int pageIndex) {
		String countSql = "select count(*) as count from ("+sql+") as mysqlcountbiemin";
		int totalRows = super.getJdbcTemplate().queryForInt(countSql,param.toArray(new String[param.size()]));
		
		Pagination pagination = queryListMapForPageNoCount(sql, param, pageSize, pageIndex);
		pagination.setTotalRows(totalRows);
		
		return pagination;
	}

	@Override
	public Pagination queryListMapForPageNoCount(String sql, List param, int pageSize, int pageIndex) {
		if (Const.listIsEmpty(param)) {
			param = new ArrayList();
		}
		sql = sql + " limit ?,? ";
		pageIndex = pageIndex < 1 ? 0 :pageIndex-1;
		param.add(pageIndex);
		param.add(pageSize);
		List req = super.getJdbcTemplate().queryForList(sql, param.toArray());
		
		Pagination pagination = new Pagination();
		pagination.setList(req);
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		
		return pagination;
	}
	
	@Override
	public Pagination queryListBeanForPage(Class bean, Map paramMap, int pageSize, int pageIndex) {
		String sql = MySQLUtils.buildSelectSql(bean);
		String countSql = "select count(*) as count from ("+sql+") as mysqlcountbiemin";
		int totalRows = this.getNamedParameterJdbcTemplate().queryForInt(countSql, paramMap);
		
		Pagination pagination = queryListBeanForPageNoCount(bean, paramMap, pageSize, pageIndex);
		pagination.setTotalRows(totalRows);
		
		return pagination;
	}
	
	@Override
	public Pagination queryListBeanForPageNoCount(Class bean, Map paramMap, int pageSize, int pageIndex) {
		String sql = MySQLUtils.buildSelectSql(bean);
		sql = sql + " limit :pageIndex,:pageSize ";
		pageIndex = pageIndex < 1 ? 0 :pageIndex-1;
		paramMap.put("pageIndex", pageIndex);
		paramMap.put("pageSize", pageSize);
		
		List req = this.getNamedParameterJdbcTemplate().queryForList(sql, paramMap, bean);
		Pagination pagination = new Pagination();
		pagination.setList(req);
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		
		return pagination;
	}

	@Override
	public int update(String sql, List param) {
		return super.getJdbcTemplate().update(sql, param.toArray());
	}

	@Override
	public int add(Object bean) {
		String sql = MySQLUtils.buildInsertSql(bean.getClass());
        SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
        return getNamedParameterJdbcTemplate().update(sql, sps);
	}
	
	@Override
	public Object get(Object bean){
		String sql = MySQLUtils.buildSelectSqlByPK(bean.getClass());
		SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
		List<Map<String, Object>> req = getNamedParameterJdbcTemplate().queryForList(sql, sps);
		if (Const.listIsEmpty(req)) {
			return null;
		}
		return MySQLUtils.coverMapToBean(req.get(0), bean.getClass());
	}

	@Override
	public int edit(Object bean) {
		String sql = MySQLUtils.buildUpdateSql(bean.getClass());
        SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
        return this.getNamedParameterJdbcTemplate().update(sql, sps);
	}

	@Override
	public int delete(Object bean) {
		String sql = MySQLUtils.buildDeleteSql(bean.getClass());
        SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
        return this.getNamedParameterJdbcTemplate().update(sql, sps);
	}

	@Override
	public int editWithColumn(Object bean, String... fileds) {
		String sql = "";
		try {
			sql = MySQLUtils.buildUpdateSqlByColumns(bean.getClass(), fileds);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("组装SQL失败："+bean+"\n"+fileds);
		}
        SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
        return this.getNamedParameterJdbcTemplate().update(sql, sps);
        
	}
	
	@Override
	public int deleteWithColumn(Object bean, String... fileds) {
		String sql = "";
		try {
			sql = MySQLUtils.buildDeleteSqlByColumns(bean.getClass(), fileds);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("组装SQL失败："+bean+"\n"+fileds);
		}
        SqlParameterSource sps = new BeanPropertySqlParameterSource(bean);
        return this.getNamedParameterJdbcTemplate().update(sql, sps);
	}

	@Override
	public Map queryListMapJGPage(String sql, List param, int pageSize, int pageIndex) {
		if (Const.listIsEmpty(param)) {
			param = new ArrayList();
		}
		String countSql = "select count(*) as count from ("+sql+") as mysqlcountbiemin";
		int totalRows = super.getJdbcTemplate().queryForInt(countSql,param.toArray(new String[param.size()]));
		
		sql = sql + " limit ?,? ";
		pageIndex = pageIndex < 1 ? 0 :pageIndex-1;
		param.add(pageIndex);
		param.add(pageSize);
		List req = super.getJdbcTemplate().queryForList(sql, param.toArray());
		
		Map page = new HashMap();
		
		int total = totalRows/pageSize;
		total = totalRows%pageSize == 0 ?total:total+1;
		
		page.put("records", totalRows);
		page.put("total",total);
		page.put("page", pageIndex + 1);
		page.put("rows", req);
		
		return page;
	}
	
}